﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Collections.ObjectModel;
using IMDemo.Models;
using agsXMPP;
using agsXMPP.protocol.client;
using agsXMPP.Collections;

namespace IMDemo.Views
{
    /// <summary>
    /// UCDialog.xaml 的交互逻辑
    /// </summary>
    public partial class UCDialog : UserControl
    {
        private XmppClientConnection xmppClient = null;

        #region 属性
        private ObservableCollection<MessageModel> messageList;

        public ObservableCollection<MessageModel> MessageList
        {
            get
            {
                if (this.messageList == null)
                {
                    this.messageList = new ObservableCollection<MessageModel>();
                }

                return this.messageList;
            }
        }

        private ObservableCollection<MessageModel> noReadMessageList;

        public ObservableCollection<MessageModel> NoReadMessageList
        {
            get
            {
                if (this.noReadMessageList == null)
                {
                    this.noReadMessageList = new ObservableCollection<MessageModel>();
                }

                return this.noReadMessageList;
            }
        }

        private string currentMessageContent;

        public string CurrentMessageContent
        {
            get
            {
                return this.currentMessageContent;
            }

            set
            {
                this.currentMessageContent = value;
            }
        }

        private ContactModel contacter;

        public ContactModel Contacter
        {
            get
            {
                return this.contacter;
            }
        }

        private int dialogType = 0;

        /// <summary>
        /// 0.私聊 1.群聊
        /// </summary>
        public int DialogType
        {
            get
            {
                return this.dialogType;
            }

            set
            {
                this.dialogType = value;
            }
        }
        #endregion

        public UCDialog(XmppClientConnection conn)
        {
            InitializeComponent();
            this.xmppClient = conn;
            this.DataContext = this;
        }

        public UCDialog(XmppClientConnection conn, ContactModel receiver, int type) : this(conn)
        {
            this.contacter = receiver;
            this.dialogType = type;
        }

        private void btnSend_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(this.txtMessage.Text))
            {
                MessageBox.Show("不能发送空消息！");
                return;
            }

            if (this.dialogType == 0)
            {
                this.SendTextMessage();
            }
            else if (this.dialogType == 1)
            {
                this.SendTextMessageToGroup();
            }
        }

        #region 方法
        #region SendTextMessage
        private void SendTextMessage()
        {
            try
            {
                Message msgModel = new Message(this.Contacter.ContactJid, Config.LoginUser, MessageType.chat, this.txtMessage.Text, null, null);
                this.xmppClient.Send(msgModel);
                txtMessage.Text = "";
                txtMessage.Focus();

                this.MessageCallback(null, msgModel, null);
            }
            catch (Exception ex)
            {
                log4net.LogManager.GetLogger(this.GetType()).Error(ex);
                MessageBox.Show("发送失败");
            }
        }
        #endregion

        #region SendTextMessageToGroup
        private void SendTextMessageToGroup()
        {
            try
            {
                Message msgModel = new Message(this.Contacter.ContactJid, Config.LoginUser, MessageType.groupchat, this.txtMessage.Text, null, null);
                this.xmppClient.Send(msgModel);
                txtMessage.Text = "";
                txtMessage.Focus();
            }
            catch (Exception ex)
            {
                log4net.LogManager.GetLogger(this.GetType()).Error(ex);
                MessageBox.Show("发送失败");
            }
        }
        #endregion

        private void MessageCallback(object sender, agsXMPP.protocol.client.Message msg, object data)
        {
            this.Dispatcher.BeginInvoke(new Action(() =>
            {
                MessageModel model = new Models.MessageModel()
                {
                    From = new Models.ContactModel()
                    {
                        ContactJid = msg.From,
                        ContactName = msg.From.Bare == Config.LoginUser.Bare ? "我" : msg.From.Bare
                    },
                    Msg = msg
                };
                this.MessageList.Add(model);
            }));
        }
        #endregion
    }
}
